package com.xu.system.auth;

import com.xu.system.auth.code.CodeAuthenticationProvider;
import com.xu.system.auth.pwd.PwdAuthenticationProvider;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.authentication.configuration.GlobalAuthenticationConfigurerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 安全配置
 *
 * @Author: 许乾伟
 * @Date: 2024/2/27 14:25
 */
@Configuration
public class SecurityConfig {
    @Resource
    AuthenticationConfiguration authenticationConfiguration;
    @Resource
    PwdAuthenticationProvider pwdAuthenticationProvider;

    @Resource
    CodeAuthenticationProvider codeAuthenticationProvider;
    /**
     * 获取AuthenticationManager（认证管理器），登录时认证使用
     * @return
     * @throws Exception
     */
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        final List<GlobalAuthenticationConfigurerAdapter> configurers = new ArrayList<>();
        //自定义认证器
        configurers.add(new GlobalAuthenticationConfigurerAdapter() {
            @Override
            public void configure(AuthenticationManagerBuilder auth) throws Exception {
                //账号密码认证器
                auth.authenticationProvider(pwdAuthenticationProvider);
                //验证码认证器
                auth.authenticationProvider(codeAuthenticationProvider);

            }
        });
        authenticationConfiguration.setGlobalAuthenticationConfigurers(configurers);
        return authenticationConfiguration.getAuthenticationManager();
    }
}
